import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class Widget(QWidget):
    def __init__(self,parent=None):
        super(Widget,self).__init__(parent)
        self.justdoubleClicked=False
        self.key=""
        self.text=""
        self.message=""
        self.resize(400,300)
        self.move(100,100)
        self.setWindowTitle("事件")
        #为了避免窗口大小重绘事件的影响
        QTimer.singleShot(0,self.giveHlep)

    def giveHlep(self):
        self.text="请点击这里触发鼠标的追踪功能"
        self.update()   #重绘事件触发paintEvent函数

    def paintEvent(self,event):
        text=self.text
        i=text.find("\n\n")
        if i>=0:
            text=text[0:i]
        if self.key:  #若触发了键盘的按钮文本信息会记录这个按钮信息
            text+="\n\n你按下了:{0}".format(self.key)
        painter=QPainter(self)
        painter.setRenderHint(QPainter.TextAntialiasing)
        painter.drawText(self.rect(),Qt.AlignCenter,text)
        if self.message:      #消息文本如果存在则在底部绘制信息5秒钟后清空
            painter.drawText(self.rect(),Qt.AlignBottom|Qt.AlignCenter,self.message)
            QTimer.singleShot(10000,self.clearMessage)
            QTimer.singleShot(10000,self.update)

    def clearMessage(self):
        self.message=""
    #单击鼠标的事件
    def mouseReleaseEvent(self, event):
        #若鼠标释放就是双击 不追踪鼠标移动
        #点击 需要改变跟踪状态
        if self.justdoubleClicked:
            self.justdoubleClicked=False
        else:
            self.setMouseTracking(not self.hasMouseTracking())#单击鼠标
            if self.hasMouseTracking():
                self.text="开启鼠标跟踪.\n"+\
                    "移动鼠标\n"+\
                    "单击鼠标关闭这个功能"
            else:
                self.text="关闭鼠标跟踪\n"+\
                    "单击鼠标开启这个功能"
            self.update()
    #鼠标移动
    def mouseMoveEvent(self, event):
        if not  self.justdoubleClicked:
            pos=self.mapToGlobal(event.pos())
            self.text="""
            窗口的坐标:QPoint({0},{1})
            屏幕的坐标:QPoint({2},{3})
            
            """.format(event.pos().x(),event.pos().y(),pos.x(),pos.y())
            self.update()
    def closeEvent(self,event):
        print("关闭")

    def resizeEvent(self,event):
        self.text="调整窗口的大小Qsize({0},{1})".format(event.size().width(),event.size().height())
        self.update()
    #上线文菜单
    def contextMenuEvent(self,event):
        menu=QMenu(self)
        newaction=menu.addAction("new")
        saveaction = menu.addAction("save")
        newaction.triggered.connect(self.new)
        saveaction.triggered.connect(self.save)
        if not self.message:
            menu.addSeparator()
            threeAction=menu.addAction("three")
            threeAction.triggered.connect(self.three)
        menu.exec_(event.pos())
    def new(self):
        self.message="new"
        self.update()
    def save(self):
        self.message = "save"
        self.update()
    def three(self):
        self.message = "three"
        self.update()

    #键盘事件
    def keyPressEvent(self, event):
        self.key=""
        if event.key()==Qt.Key_End:
            self.key="end"
        else:
            QWidget.keyPressEvent(self,event)
if __name__ == '__main__':
    app=QApplication(sys.argv)
    demo=Widget()
    demo.show()
    app.exec_()